package com.asiainfo.iov.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asiainfo.iov.configs.InterfaceConfig;
import com.asiainfo.iov.networkModel.HttpUtil;
import com.framework.utils.KeyBoardUtil;
import com.framework.utils.RegularUtil;
import com.framework.utils.ScreenUtils;
import com.framework.utils.ToastUtil;
import com.framework2.customviews.TitleView;
import com.framework2.util.CustomLoadingDialogUtils;
import com.framework2.util.PicToastUtil;
import com.yaxin.yyt.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改密码
 *
 * @author YobertJomi
 * className ModifyPasswordActivity
 * created at  2017/3/15  11:30
 */
public class ModifyPasswordActivity extends BaseActivity {
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.et_pwd_pre)
    EditText etPwdPre;
    @BindView(R.id.etPwdNew)
    EditText etPwdNew;
    @BindView(R.id.etPwdNewAgin)
    EditText etPwdNew_agin;
    @BindView(R.id.checkbox)
    CheckBox checkbox;//显示密码
    @BindView(R.id.showPwdLayout)
    LinearLayout showPwdLayout;
    @BindView(R.id.tv_sure)
    TextView tvSure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        ButterKnife.bind(this);
        ScreenUtils.getInstance().setTranslucentStatus(this, true);
        ScreenUtils.getInstance().setStatusBarTintColor(this,
                getResources().getColor(R.color.white));
        initView();
    }

    private void initView() {
        titleView.setTitle(R.string.modifyPwd).setRightVisible(false);
    }

    private boolean isLegal(EditText et_pwd_pre, EditText etPwdNew, EditText etPwdNew_agin) {
        if (TextUtils.isEmpty(et_pwd_pre.getText())) {
            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "请输入原密码!");
            et_pwd_pre.requestFocus();
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(ModifyPasswordActivity.this, et_pwd_pre, false);
            return false;
        } else if (TextUtils.isEmpty(etPwdNew.getText())) {
            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "请输入新密码!");
            etPwdNew.requestFocus();
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(ModifyPasswordActivity.this, et_pwd_pre, false);
            return false;
        }
//        else if (RegularUtil.getInstance().isChineseChar(etPwdNew.getText().toString())) {
//            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "新密码不能包含中文字符!");
//            etPwdNew.requestFocus();
//            Utils.isCloseSoftInputMethod(ModifyPasswordActivity.this, etPwdNew, false);
//            return false;
//        }
        else if (!RegularUtil.getInstance().isCharOrNumber(etPwdNew.getText().toString())) {
            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "新密码须6-20位数字或字母!");
            etPwdNew.requestFocus();
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(ModifyPasswordActivity.this, etPwdNew, false);
            return false;
        } else if (TextUtils.isEmpty(etPwdNew_agin.getText())) {
            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "请再次输入新密码!");
            etPwdNew_agin.requestFocus();
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(ModifyPasswordActivity.this, etPwdNew_agin, false);
            return false;
        }
//        else if (!Utils.isCharOrNumber(etPwdNew_agin.getText().toString())) {
//            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "第二次密码新密码须6-20位数字或字母!");
//            etPwdNew_agin.requestFocus();
//            Utils.isCloseSoftInputMethod(ModifyPasswordActivity.this, etPwdNew_agin, false);
//            return false;
//        }
        else if (!TextUtils.equals(etPwdNew.getText().toString(), etPwdNew_agin.getText().toString())) {
            PicToastUtil.getInstance().showToast(ModifyPasswordActivity.this, "两次新密码不相同，请重新输入!");
            etPwdNew_agin.setText("");
            etPwdNew_agin.requestFocus();
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(ModifyPasswordActivity.this, etPwdNew_agin, false);
            return false;
        }
        return true;
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.showPwdLayout:
                if (!checkbox.isChecked()) {
                    checkbox.setChecked(true);
                    //如果选中，显示密码
                    etPwdPre.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etPwdNew.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etPwdNew_agin.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etPwdPre.setSelection(etPwdPre.getText().toString().length());
                    etPwdNew.setSelection(etPwdNew.getText().toString().length());
                    etPwdNew_agin.setSelection(etPwdNew_agin.getText().toString().length());
                } else {
                    checkbox.setChecked(false);
                    //否则隐藏密码
                    etPwdPre.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etPwdNew.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etPwdNew_agin.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etPwdPre.setSelection(etPwdPre.getText().toString().length());
                    etPwdNew.setSelection(etPwdNew.getText().toString().length());
                    etPwdNew_agin.setSelection(etPwdNew_agin.getText().toString().length());
                }
                break;
            case R.id.tv_sure:
                if (isLegal(etPwdPre, etPwdNew, etPwdNew_agin)) {
                    KeyBoardUtil.getInstance().isCloseSoftInputMethod(this, etPwdPre, true);
                    requestModifyPwd();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 请求修改密码
     */
    private void requestModifyPwd() {
        CustomLoadingDialogUtils.showProcessDialog(ModifyPasswordActivity.this, "正在修改密码");
        HttpUtil.getInstance().requestModifyPwd(InterfaceConfig.modifyPwd, etPwdPre.getText().toString(), etPwdNew
                        .getText().toString(), etPwdNew_agin.getText().toString(),
                new HttpUtil.OnRequestResult<String>() {
                    @Override
                    public void onSuccess(String... s) {
                        CustomLoadingDialogUtils.dismissProcessDialog();
                        ToastUtil.getInstance().showToast("修改密码成功");
                        finishActivity();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        CustomLoadingDialogUtils.dismissProcessDialog();
                        ToastUtil.getInstance().showToast(TextUtils.isEmpty(msg) ? "修改密码失败" : msg);
                    }
                });
    }
}
